package com.whtq.front.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by
 *
 * @author wmi
 * @date 2019/12/17 - 11:28.
 * @description
 */
public class DateUtil {
    protected static final Logger LOG = LoggerFactory.getLogger(DateUtil.class);

    private static final String DATEPATTERN = "yyyy-MM-dd";

    private static final String DATEPATTERN_YYYYMMdd = "yyyyMMdd";

    private static final String DATETIMEPATTERN = "yyyy-MM-dd HH:mm:ss";

    private static final String TIMEPATTERN = "HH:mm";

    private final static DateFormat FORMAT_YYYYMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss");

    private final static DateFormat FORMAT_YYMMddHHmmss = new SimpleDateFormat("yyMMddHHmmss");

    public final static DateFormat FORMAT_YYYYMMDD = new SimpleDateFormat("yyyyMMdd");

    public final static DateFormat FORMAT_YYYY = new SimpleDateFormat("yyyy");

    public final static DateFormat FORMAT_MM = new SimpleDateFormat("MM");

    public final static DateFormat FORMAT_HHMMSS = new SimpleDateFormat("HHmmss");

    public final static DateFormat FORMAT_HH = new SimpleDateFormat("HH");

    public final static DateFormat FORMAT_dd = new SimpleDateFormat("dd");


    /**
     * Return 缺省的日期格式 (yyyy/MM/dd)
     *
     * @return 在页面中显示的日期格式
     */
    public static String getDATEPATTERN() {
        return DATEPATTERN;
    }

    /**
     * 根据日期格式，返回日期按DATEPATTERN格式转换后的字符串
     *
     * @param aDate 日期对象
     * @return 格式化后的日期的页面显示字符串
     */
    public static final String getDate(Date aDate) {
        SimpleDateFormat df = null;
        String returnValue = "";

        if (aDate != null) {
            df = new SimpleDateFormat(DATEPATTERN);
            returnValue = df.format(aDate);
        }

        return (returnValue);
    }

    /**
     * 根据日期格式，返回日期按DATEPATTERN格式转换后的字符串
     *
     * @param aDate 日期对象
     * @return 格式化后的日期的页面显示字符串
     */
    public static final String getDateTime(Date aDate) {
        SimpleDateFormat df = null;
        String returnValue = "";

        if (aDate != null) {
            df = new SimpleDateFormat(DATETIMEPATTERN);
            returnValue = df.format(aDate);
        }

        return (returnValue);
    }

    /**
     * 按照日期格式，将字符串解析为日期对象
     *
     * @param aMask   输入字符串的格式
     * @param strDate 一个按aMask格式排列的日期的字符串描述
     * @return Date 对象
     * @see SimpleDateFormat
     */
    public static final Date convertStringToDate(String aMask, String strDate) {
        SimpleDateFormat df = null;
        Date date = null;
        df = new SimpleDateFormat(aMask);

        if (LOG.isDebugEnabled()) {
            LOG.debug("converting '" + strDate + "' to date with mask '" + aMask + "'");
        }

        try {
            date = df.parse(strDate);
        } catch (ParseException pe) {
            LOG.error("ParseException: " + pe);
        }

        return (date);
    }

    /**
     * This method returns the current date time in the format: yyyy/MM/dd HH:MM
     * a
     *
     * @param theTime the current time
     * @return the current date/time
     */
    public static String getTimeNow(Date theTime) {
        return getDateTime(TIMEPATTERN, theTime);
    }

    /**
     * This method returns the current date in the format: yyyy-MM-dd
     *
     * @return the current date
     * @throws ParseException
     */
    public static Calendar getToday() throws ParseException {
        Date today = new Date();
        SimpleDateFormat df = new SimpleDateFormat(DATEPATTERN);

        // This seems like quite a hack (date -> string -> date),
        // but it works ;-)
        String todayAsString = df.format(today);
        Calendar cal = new GregorianCalendar();
        cal.setTime(convertStringToDate(todayAsString));

        return cal;
    }

    /**
     * This method generates a string representation of a date's date/time in
     * the format you specify on input
     *
     * @param aMask the date pattern the string is in
     * @param aDate a date object
     * @return a formatted string representation of the date
     * @see SimpleDateFormat
     */
    public static final String getDateTime(String aMask, Date aDate) {
        SimpleDateFormat df = null;
        String returnValue = "";

        if (aDate == null) {
            LOG.error("aDate is null!");
        } else {
            df = new SimpleDateFormat(aMask);
            returnValue = df.format(aDate);
        }

        return (returnValue);
    }

    /**
     * 根据日期格式，返回日期按DATEPATTERN格式转换后的字符串
     *
     * @param aDate
     * @return
     */
    public static final String convertDateToString(Date aDate) {
        return getDateTime(DATEPATTERN, aDate);
    }

    /**
     * 根据日期格式，返回日期按DATETIMEPATTERN格式转换后的字符串
     *
     * @param aDate
     * @return
     */
    public static final String convertDateTimeToString(Date aDate) {
        return getDateTime(DATETIMEPATTERN, aDate);
    }

    /**
     * 按照日期格式，将字符串解析为日期对象
     *
     * @param strDate (格式 yyyyMMdd)
     * @return
     * @throws ParseException
     */
    public static Date convertYYYYMMDDToDate(String strDate) {
        Date aDate = null;
        if (LOG.isDebugEnabled()) {
            LOG.debug("converting date with pattern: " + DATEPATTERN);
        }
        aDate = convertStringToDate(DATEPATTERN, strDate);
        return aDate;
    }



    /**
     * 按照日期格式，将字符串解析为日期对象
     *
     * @param strDate (格式 yyyy-MM-dd)
     * @return
     * @throws ParseException
     */
    public static Date convertStringToDate(String strDate) {
        Date aDate = null;

        if (LOG.isDebugEnabled()) {
            LOG.debug("converting date with pattern: " + DATEPATTERN);
        }

        aDate = convertStringToDate(DATEPATTERN, strDate);

        return aDate;
    }

    /**
     * 按照日期格式，将字符串解析为日期对象
     *
     * @param strDate (格式 yyyy-MM-dd HH:mm:ss)
     * @return
     * @throws ParseException
     */
    public static Date convertTimeStringToDate(String strDate) {
        Date aDate = null;

        if (LOG.isDebugEnabled()) {
            LOG.debug("converting date with pattern: " + DATETIMEPATTERN);
        }

        aDate = convertStringToDate(DATETIMEPATTERN, strDate);

        return aDate;
    }

    /**
     * 时间相加
     *
     * @param date
     * @param day
     * @return
     */
    public static Date dateAdd(Date date, int day) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, day);
        return calendar.getTime();
    }

    /**
     * 获取两个日期之间的天数
     *
     * @param date1
     * @param date2
     * @return
     */
    public static long dateDiffer(Date date1, Date date2) {
        return (date1.getTime() - date2.getTime()) / (1000 * 3600 * 24);
    }

    /**
     * 得到两个日期之间的天数差，包括开始和结束日期(如：beginCalender=2007-10-01，endCalendar=2007-10-20
     * 结果为：20)
     *
     * @param beginDay 开始日期(小的)
     * @param endDay   结束日期(大的)
     * @return
     */
    public static Long getDifferenceDays(Date beginDay, Date endDay) {
        Calendar beginCalender = Calendar.getInstance();
        beginCalender.setTime(beginDay);

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDay);

        Long days = (endCalendar.getTimeInMillis() - beginCalender.getTimeInMillis()) / (24 * 60 * 60 * 1000);
        days = days + 1;
        return days;
    }

    public static Date getFirstDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }

    public static Date getLastDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    /**
     * 返回本月最后一天
     *
     * @param date
     * @return
     */
    public static String getLastDayStr(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 0);
        DateFormat format = new SimpleDateFormat(DATEPATTERN_YYYYMMdd);
        return format.format(cal.getTime());
    }

    /**
     * 返回日期String格式 yyyyMMdd
     *
     * @param date
     * @return
     */
    public static String getDateStr(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        DateFormat format = new SimpleDateFormat(DATEPATTERN_YYYYMMdd);
        return format.format(cal.getTime());
    }


    public static String getDateStryyyyMMddHMS(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        DateFormat format = new SimpleDateFormat(DATETIMEPATTERN);
        return format.format(cal.getTime());
    }





    /**
     * 返回本月第一天
     *
     * @param date  日期
     * @param month 0 当月 1下月 -1 上月 以此类推
     * @return
     */
    public static String getFirstDateStr(int month, Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.MONTH, month);
        DateFormat format = new SimpleDateFormat(DATEPATTERN_YYYYMMdd);
        return format.format(cal.getTime());
    }

    /**
     * 得到当前时间,格式为yyyyMMddHHmmss
     *
     * @return
     */
    public static String getyyyyMMddHHmmssCurDate() {
        return FORMAT_YYYYMMddHHmmss.format(new Date());
    }

    public static String getyyMMddHHmmssCurDate() {
        return FORMAT_YYMMddHHmmss.format(new Date());
    }


    public static String getAferyyMMddHHmmss(String curTime,long time) {
        Date now = new Date();
        Date afterDate = new Date(now.getTime() + 60000 * time);
        System.out.println(FORMAT_YYYYMMddHHmmss.format(afterDate ));
        return FORMAT_YYYYMMddHHmmss.format(afterDate );
    }





    /**
     * date转换成calendar，只精确到yyyyMMdd
     *
     * @throws ParseException
     */
    public static Calendar getyyyyMMDDCalendar(Date date) throws ParseException {
        SimpleDateFormat dataFormat = new SimpleDateFormat(DATEPATTERN_YYYYMMdd);
        dataFormat.parse(dataFormat.format(date));
        return dataFormat.getCalendar();
    }

    /**
     * 获取日期毫秒数(精确到天)
     *
     * @throws ParseException
     */
    public static long getyyyyMMDDTimeInMillis(Date date) throws ParseException {
        return getyyyyMMDDCalendar(date).getTimeInMillis();
    }

    /**
     * 获取日期的HHmmss
     *
     * @param date
     * @return
     */
    public static long getFormatHHmmss(Date date) {
        return Long.valueOf(getFormatHHmmssString(date));
    }

    public static String getFormatHHmmssString(Date date) {
        return FORMAT_HHMMSS.format(date);
    }

    public static String getFormatYYYYMMDD(Date date) {
        return FORMAT_YYYYMMDD.format(date);
    }

    public static String getFormatYYYY(Date date) {
        return FORMAT_YYYY.format(date);
    }

    public static String getFormatMM(Date date) {
        return FORMAT_MM.format(date);
    }

    public static String getFormatdd(Date date) {
        return FORMAT_dd.format(date);
    }

    public static Date getCurDate() {
        return new Date();
    }

    /**
     * 获取日期的HH
     *
     * @param date
     * @return
     */
    public static long getFormatHH(Date date) {
        return Long.valueOf(FORMAT_HH.format(date));
    }

    /**
     * 时间是否在<code>days</code>内
     *
     * @param old
     * @param days
     * @return
     */
    public static boolean isDaysInterval(Date old, int days) {
        Calendar now = Calendar.getInstance();
        return (now.getTimeInMillis() - old.getTime()) <= (1000L * 3600 * 24 * days);
    }

    /**
     * 得到当前日期后的N天的日期
     *
     * @param days 天数
     * @return
     */
    public static Date getBackDaysOfNow(int days) {
        Calendar now = Calendar.getInstance();
        now.setTimeInMillis(now.getTimeInMillis() + 1000L * 3600 * 24 * days);
        return now.getTime();
    }

    public static void main(String[] args) {
//        LOG.info(getLastDayStr(new Date()));
//        LOG.info(getFirstDateStr(-1, new Date()));
        LOG.info(getyyyyMMddHHmmss("2016-12-20 21:09:25"));


    }

    public static String getyyyyMMddHHmmss(String strDate) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATETIMEPATTERN);

        String StringDate ="";
        try {
            StringDate =  FORMAT_YYYYMMddHHmmss.format(sdf.parse(strDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return StringDate;
    }

    public static Date getBackDaysOfDay(Date date, int days) {
        Calendar day = Calendar.getInstance();
        day.setTime(date);
        day.setTimeInMillis(day.getTimeInMillis() + 1000L * 3600 * 24 * days);
        return day.getTime();
    }

    public static Date getBeginOfDay(Date day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(day);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    // 获取下一年日期
    public static String getAddYearDate(String strDate) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        Calendar c = Calendar.getInstance();

        if (strDate == null || strDate == "") {
            c.setTime(new Date());
        } else {
            c.setTime(convertYYYYMMDDToDate(strDate));
        }

        c.add(Calendar.YEAR, +1);
        Date m = c.getTime();
        return format.format(m);
    }

    /**
     * 在日期上增加n分钟
     *
     * @param date
     * @param n
     * @return
     */
    public static Date addMinute(Date date, int n) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, n);
        return cal.getTime();
    }

    //比较时间date1>date2
    public static boolean compareDate(Date date1, Date date2) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        Long ctime = calendar.getTimeInMillis();
        calendar.setTime(date2);
        Long endTime = calendar.getTimeInMillis();
        return ctime > endTime;
    }

    public static Date parseDateNewFormat(String sDate) {
        DateFormat dateFormat = new SimpleDateFormat(DATETIMEPATTERN);
        Date d = null;
        if ((sDate != null) && (sDate.length() == DATETIMEPATTERN.length())) {
            try {
                d = dateFormat.parse(sDate);
            } catch (ParseException ex) {
                return null;
            }
        }
        return d;
    }

    public static boolean inSameDay(Date date1, Date Date2) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        int year1 = calendar.get(Calendar.YEAR);
        int month1 = calendar.get(Calendar.MONTH);
        int day1 = calendar.get(Calendar.DAY_OF_YEAR);

        calendar.setTime(Date2);
        int year2 = calendar.get(Calendar.YEAR);
        int month2 = calendar.get(Calendar.MONTH);
        int day2 = calendar.get(Calendar.DAY_OF_YEAR);

        if ((year1 == year2) && (month1 == month2) && (day1 == day2)) {
            return true;
        }
        return false;
    }


    public static String defaultDatePattern = "yyyyMMdd";
    public static final String timePattern = "HHmmss";

    /**
     * 返回 当前日期字符串
     *
     * @return yyyyMMdd
     */
    public static String getTodayStr() {
        Date today = new Date();
        return format(today);
    }

    public static String getTime() {
        Date today = new Date();
        return format(today, timePattern);
    }

    public static String getDate() {
        Date today = new Date();
        return format(today, defaultDatePattern);
    }

    /**
     * 获得默认的 date pattern
     */
    public static String getDatePattern() {
        return defaultDatePattern;
    }

    /**
     * 使用预设Format格式化Date成字符串
     */
    public static String format(Date date) {
        return date == null ? "" : format(date, getDatePattern());
    }
    /**
     * 使用参数Format格式化Date成字符串
     */
    public static String format(Date date, String pattern) {
        return date == null ? "" : new SimpleDateFormat(pattern).format(date);
    }
    private static SimpleDateFormat defaultFormat = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");

    public static String getNow() {
        Date date = new Date();
        return defaultFormat.format(date).toString();
    }

    public static String getNow(String formatStr) {
        Date date = new Date();
        try {
            SimpleDateFormat format = new SimpleDateFormat(formatStr);
            return format.format(date).toString();
        }
        catch (Exception e) {
        }
        return defaultFormat.format(date);
    }

    public static String getDate(Date date, String formatStr) {
        SimpleDateFormat format;
        try {
            format = new SimpleDateFormat(formatStr);
            return format.format(date).toString();
        }
        catch (Exception e) {
        }
        return defaultFormat.format(date);
    }

    public static Date parse(String date) throws ParseException {
        return defaultFormat.parse(date);
    }

    public static String getTenDate(){
        Date now = new Date();
        Date now_10 = new Date(now.getTime() - 1800000); //30分钟前的时间
        SimpleDateFormat dateFormat = new SimpleDateFormat("HHmmss");//可以方便地修改日期格式
        String nowTime_10 = dateFormat.format(now_10);
        return nowTime_10;
    }

    public static Date parse(String date, String formatStr)
            throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        return format.parse(date);
    }

    public static String getDuringTime() {
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("HH");
        String str = df.format(date);
        int a = Integer.parseInt(str);
        if (a >= 0 && a < 12) {
            return "早上好！";
        }else if (a >= 12 && a < 19) {
            return "下午好！";
        }else{
            return "晚上好！";
        }
    }

    public static int getCurreYear(){
        Calendar cal = Calendar.getInstance();
        return  cal.get(Calendar.YEAR);
    }
}
